package com.societyGames.codeAlong.state.codeExercise.model
{
/*at com.societyGames.codeAlong.state.codeExercise.view::EditorView/executeClickHandler()[G:\Projects\CodeAlong\CodeAlong\src\com\societyGames\codeAlong\state\codeExercise\view\EditorView.as:99]*/

public class StackTraceCallMessage
{
  private var _atWithTabs:String;
  private var _functionName:String;
  private var _sourceFile:String; //may be null
  private var _lineNumber:int;

  public function toString():String
  {
    if (_sourceFile != null)
    {
      return _atWithTabs + " " + _functionName + "[" + _sourceFile + ":" + (_lineNumber + 1).toString() + "]";
    }
    else
    {
      return _atWithTabs + " " + _functionName;
    }
  }

  public static function fromString(text:String):StackTraceCallMessage
  {
    var message:StackTraceCallMessage = new StackTraceCallMessage();
    var textParts:Array = text.split("[");
    //Function name
    var functionParts:Array = textParts[0].split(" ");
    if (functionParts.length < 2)
    {
      throw new ArgumentError("Invalid text.");
    }
    message._atWithTabs = functionParts[0];
    var functionNameParts:Array = functionParts.slice(1); //function name may have had spaces and could be split across parts.
    message._functionName = functionNameParts.join(" "); //Put the function name parts back together.
    if (textParts.length > 1)
    {
      //FileName
      var sourceAndLineNumber:String = textParts[1];
      //Drop the last "]"
      if (sourceAndLineNumber.lastIndexOf("]") != sourceAndLineNumber.length - 1)
      {
        throw new ArgumentError("Invalid text.");
      }
      sourceAndLineNumber = sourceAndLineNumber.substring(0, sourceAndLineNumber.length - 1); //Chop off final ]
      var sourceAndLineColonIndex:int = sourceAndLineNumber.lastIndexOf(":");
      message._sourceFile = sourceAndLineNumber.substring(0, sourceAndLineColonIndex);
      message._lineNumber = int(sourceAndLineNumber.substring(sourceAndLineColonIndex + 1)) - 1;
    }
    return message;
  }

  public function get atWithTabs():String
  {
    return this._atWithTabs;
  }

  public function get functionName():String
  {
    return this._functionName;
  }

  public function get sourceFile():String //may be null.
  {
    return this._sourceFile;
  }

  public function get lineNumber():int
  {
    return this._lineNumber;
  }

  public function set lineNumber(value:int):void
  {
    this._lineNumber = value;
  }
}
}